
#include "StaticErrorInfo.h"
#include <stdlib.h>
#include <assert.h>
#include <string>

static int cmp_info( const void *info1, const void *info2 )
{
	int code1 = ((struct error_info_t *)info1)->code;
	int code2 = ((struct error_info_t *)info2)->code;

	return code1 < code2 ? -1 : ( code1 == code2 ? 0 : 1 );
}

int StaticErrorInfo::FindError( int code, struct error_info_t &info )
{
	assert( NULL != mp_info );
	error_info_t key, *res;
	key.code = code;
	// since error infos in mp_info are sorted, so no need qsort
	res = (struct error_info_t *)bsearch( &key, mp_info, m_err_size, sizeof(struct error_info_t), cmp_info );
	if( NULL == res )
		return -1;
	info = *res;

	return 0;
}

/*
   int StaticErrorInfo::FindError( int code, struct error_info_t &info )
   {
	assert( NULL != mp_info );
	const struct error_info_t *p_info = mp_info;
	for( int i = 0; i < m_err_size; p_info++, i++ ) {
		if( p_info->code == code ) {
			info = *p_info;
			return 0;
		}
	}
	return -1;
   }
 */

